package com.xiaowen.ssyx.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaowen.ssyx.acl.mapper.RoleMapper;
import com.xiaowen.ssyx.acl.service.AdminRoleService;
import com.xiaowen.ssyx.acl.service.RoleService;
import com.xiaowen.ssyx.model.acl.AdminRole;
import com.xiaowen.ssyx.model.acl.Role;
import com.xiaowen.ssyx.vo.acl.RoleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
        implements RoleService {

    @Autowired
    private AdminRoleService adminRoleService;

    /**
     * 角色分页查询
     */
    @Override
    public IPage<Role> selectRolePage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {
        String roleName = roleQueryVo.getRoleName();
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(roleName)) {
            queryWrapper.like(Role::getRoleName, roleName);
        }
        return baseMapper.selectPage(pageParam, queryWrapper);
    }

    /**
     * 根据用户获取角色数据
     */
    @Override
    public Map<String, Object> findRoleByUserId(Long adminId) {
        //查询所有的角色
        List<Role> allRolesList = baseMapper.selectList(null);

        //拥有的角色id
        List<AdminRole> existUserRoleList = adminRoleService.list(new LambdaQueryWrapper<AdminRole>().
                eq(AdminRole::getAdminId, adminId)
                .select(AdminRole::getRoleId));
        List<Long> existRoleList = existUserRoleList.stream().map(item ->
                item.getRoleId()).collect(Collectors.toList());

        //对角色进行分类
        List<Role> assignRoles = new ArrayList<Role>();
        for (Role role : allRolesList) {
            //已分配
            if (existRoleList.contains(role.getId())) {
                assignRoles.add(role);
            }
        }

        Map<String, Object> roleMap = new HashMap<>();
        roleMap.put("assignRoles", assignRoles);
        roleMap.put("allRolesList", allRolesList);
        return roleMap;
    }

    /**
     * 分配角色
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUserRoleRealTonShip(Long adminId, Long[] roleIds) {
        // 删除原来分配的角色
        adminRoleService.remove(new LambdaQueryWrapper<AdminRole>().eq(AdminRole::getAdminId, adminId));
        // 分配新的角色
        List<AdminRole> userRoleList = new ArrayList<>();
        for (Long roleId : roleIds) {
            AdminRole adminRole = new AdminRole();
            adminRole.setRoleId(roleId);
            adminRole.setAdminId(adminId);
            userRoleList.add(adminRole);
        }
        adminRoleService.saveBatch(userRoleList);
    }
}